<?php

namespace app\pc\controller;
use think\Controller;
use wxpay\database\WxPayResults;
use wxpay\database\WxPayUnifiedOrder;
use wxpay\NativePay;
use wxpay\WxPayApi;
use wxpay\WxPayConfig;

class Wxpay  extends Controller
{

    public function index()
    {
        //TODO 进行业务处理

        $params = input('param.');
        $product_name =$params['product_name'];
        $totalfee =$params['TotalFee'];
        $user_id=$params['user_id'];
        $goods_id=$params['goods_id'];
		
        //TODO 生成二维码
        $order_id = time()+1;
        $notify = new NativePay();
        $input = new WxPayUnifiedOrder();
        $input->setBody($product_name);
        $input->setAttach("xxx");
        //$input->setOutTradeNo(WxPayConfig::MCHID.date("YmdHis"));
        $input->setOutTradeNo($order_id);
        $input->setTotalFee($totalfee);//以分为单位
        $input->setTimeStart(date("YmdHis"));
        $input->setTimeExpire(date("YmdHis", time() + 600));
        $input->setGoodsTag("test");

        $input->setNotifyUrl(wxPayConfig::NOTIFY_URL);
        $input->setTradeType("NATIVE");
        //$product_id 为商品自定义id 可用作订单ID
        $input->setProductId($goods_id);
        $result = $notify->getPayUrl($input);
        if (empty($result['code_url'])){
            $qrCode_url = '';
        }else{
            $qrCode_url = $result["code_url"];
            //保全订单
            $data=[
                'order_id'=>$order_id,
                'product_name'=>$product_name,
                'goods_id'=>$goods_id,
                'user_id'=>$user_id,
                'status' =>0,
                'money'=>$totalfee,
                'time'=>time()
            ];
            db("order")->insert($data);
        }
        
        return $this->fetch('index',[
            'qrCode_url' => $qrCode_url,
        ]);
}

    /**
     * 微信支付 回调逻辑处理
     * @return string
     */
    public function notify(){
	
       $wxData = file_get_contents("php://input");
      
        try{
            $resultObj = new WxPayResults();
            $wxData = $resultObj->Init($wxData);
        }catch (\Exception $e){
            $resultObj ->setData('return_code','FAIL');
            $resultObj ->setData('return_msg',$e->getMessage());
            return $resultObj->toXml();
        }

        if ($wxData['return_code']==='FAIL'||
            $wxData['return_code']!== 'SUCCESS'){
            $resultObj ->setData('return_code','FAIL');
            $resultObj ->setData('return_msg','error');
            return $resultObj->toXml();
        }
		
      

        //TODO 根据订单号 out_trade_no 来查询订单数据
        $order_id = (int)$wxData['out_trade_no'];
	
    
		
		$order=db("order")->where('order_id',$order_id)->find();
	

        if (empty($order)) {
            $resultObj ->setData('return_code','SUCCESS');
            $resultObj ->setData('return_msg','OK');
            return $resultObj->toXml();
        }

       
        //TODO 数据更新 业务逻辑处理 $order
		
       db("order")->update(['status' => '1','id'=>$order['id']]);
		
		
        //更新权限

        user_input($order['user_id'],$order['goods_id']);
    }
}
